﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;

namespace IEWebSite
{
    public class Utils
    {
        public static SqlConnection GetSqlConnection()
        {
            SqlConnection sqlConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["SiteIEConnectionString"].ConnectionString);
            return sqlConnection;
        }

        public static Int32 GetNextTableId(string tableName)
        {
            SqlConnection sqlConnection = GetSqlConnection();
            Int32 nextId = 1;

            try
            {
                sqlConnection.Open();
                SqlCommand sqlCmd = new SqlCommand("SELECT MAX(Id" + tableName + ") FROM " + tableName, sqlConnection);
                Object result = sqlCmd.ExecuteScalar();
                if (result != System.DBNull.Value)
                {
                    nextId = (Int32)result; nextId++;
                }
            }
            finally
            {
                if(sqlConnection.State != System.Data.ConnectionState.Closed) sqlConnection.Close();
            }

            return nextId;
        }
    }
}
